package com.junci.mtest.vm;

import android.annotation.SuppressLint;

import androidx.lifecycle.LiveData;
import androidx.lifecycle.MutableLiveData;
import androidx.lifecycle.ViewModel;

import io.reactivex.Observable;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;

/**
 * Created by YM
 * Create Date 2024/4/30 18:35
 * Used
 */
public class MainViewModle extends ViewModel {

    private MutableLiveData<String> myData = new MutableLiveData<>();
    private MutableLiveData<String> title = new MutableLiveData<>();

    public LiveData<String> getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title.setValue(title);
    }


    public LiveData<String> getMyData() {
        fetchDataFromNetwork();
        return myData;
    }

    /** @noinspection ResultOfMethodCallIgnored
     *
     */
    @SuppressLint("CheckResult")
    private void fetchDataFromNetwork() {
        Observable.fromCallable(() -> {
            // 模拟从网络获取数据
                    return "data";
        })
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(
                        data -> myData.setValue(data),
                        error -> myData.setValue("Error occurred")
                );
    }






}
